package com.ruoyi.employment.service.impl;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.employment.domain.TInterview;
import com.ruoyi.employment.mapper.TInterviewMapper;
import com.ruoyi.employment.service.ITInterviewService;
import com.ruoyi.student.api.ClassService;
import com.ruoyi.student.domain.TClass;
import com.ruoyi.teacher.api.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 面试登记Service业务层处理
 *
 * @author ruoyi
 * @date 2022-12-25
 */
@Service
public class TInterviewServiceImpl implements ITInterviewService
{
    @Autowired
    private TInterviewMapper tInterviewMapper;

    @Autowired
    private ClassService classService;
    /**
     * 查询面试登记
     *
     * @param id 面试登记主键
     * @return 面试登记
     */
    @Override
    public TInterview selectTInterviewById(Long id)
    {
        return tInterviewMapper.selectTInterviewById(id);
    }

    /**
     * 查询面试登记列表
     *
     * @param tInterview 面试登记
     * @return 面试登记
     */
    @Override
    public List<TInterview> selectTInterviewList(TInterview tInterview)
    {
        List<TInterview> tInterviews = tInterviewMapper.selectTInterviewList(tInterview);

        List<TClass> rows = classService.listClass();
        Map<Long, String> collect = rows.stream().collect(Collectors.toMap(TClass::getId, TClass::getName));
        tInterviews.forEach(t -> {
            if (collect.get(Long.valueOf(t.getClasses())) != null) {
                TClass tClass1 = t.gettClass();
                tClass1.setName(collect.get(Long.valueOf(t.getClasses())));
                t.settClass(tClass1);
            }
        });
        return tInterviews;
    }

    /**
     * 新增面试登记
     *
     * @param tInterview 面试登记
     * @return 结果
     */
    @Override
    public int insertTInterview(TInterview tInterview)
    {
        List<TInterview> tInterviews = tInterviewMapper.selectTInterviewByName(tInterview.getStudentName());
        if (tInterviews.size() == 0 || tInterviews == null) {
            tInterview.setCount(1L);
            return tInterviewMapper.insertTInterview(tInterview);
        } else {
            tInterviews.forEach(e -> {
                tInterview.setCount(e.getCount() + 1);
                e.setCount(e.getCount() + 1);
                updateTInterview(e);
            });
            return tInterviewMapper.insertTInterview(tInterview);
        }

    }

    /**
     * 修改面试登记
     *
     * @param tInterview 面试登记
     * @return 结果
     */
    @Override
    public int updateTInterview(TInterview tInterview)
    {
        return tInterviewMapper.updateTInterview(tInterview);
    }

    /**
     * 批量删除面试登记
     *
     * @param ids 需要删除的面试登记主键
     * @return 结果
     */
    @Override
    public int deleteTInterviewByIds(Long[] ids)
    {
        return tInterviewMapper.deleteTInterviewByIds(ids);
    }

    /**
     * 删除面试登记信息
     *
     * @param id 面试登记主键
     * @return 结果
     */
    @Override
    public int deleteTInterviewById(Long id)
    {
        return tInterviewMapper.deleteTInterviewById(id);
    }
}
